package com.bilibili.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zuife
 * @description
 * @since 2024-04-20 16:48:35
 */
public class CompletableFutureCombineDemo {
    public static void main(String[] args) {

        CompletableFuture<Integer> future1=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+ "\t"+"A...开始");
            return 10;
        });
        CompletableFuture<Integer> future2=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+ "\t"+"B...开始");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 20;
        });
        CompletableFuture<Integer> result = future1.thenCombine(future2, (r1, r2) -> {
            return r1 + r2;
        });
        System.out.println(Thread.currentThread().getName()+"\t"+"----------"+result.join());
    }
}
